package scau.erps.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;

import scau.erps.app.Erps;
import scau.erps.entity.Staff;
import scau.erps.entity.Team;
import scau.erps.service.StaffService;
import scau.erps.service.TeamService;
import scau.erps.service.impl.StaffServiceImpl;
import scau.erps.service.impl.TeamServiceImpl;
import scau.erps.view.business.BusinessFrame;
import scau.erps.view.decision.DecisionFrame;
import scau.erps.view.system.SystemManagementFrame;

/**
 * 登陆对话框
 * @author DCJ
 *
 */
public class LoginDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private JPanel contentPanel,teamPanel,staffPanel;
	private JComboBox<String> teamNameField,staffNameField;
	private JPasswordField teamPasswordField,staffPasswordField;
	private JButton teamLoginButton,trainButton,staffLoginButton;
	public LoginDialog() {
		this.setTitle("ERP沙盘");
		this.setSize(350, 300);
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		JTabbedPane tabbedPanel = new JTabbedPane(JTabbedPane.TOP);
		tabbedPanel.add(getTeamPanel(),"小组登陆");
		tabbedPanel.add(getStaffPanel(),"工作人员登陆");
		contentPanel.add(tabbedPanel, BorderLayout.CENTER);
		this.setContentPane(contentPanel);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	/**
	 * 小组登陆面板
	 * @return
	 */
	private JPanel getTeamPanel(){
		teamPanel = new JPanel();
		teamPanel.setLayout(null);
		JLabel teamLabel = new JLabel("小    组");
		teamLabel.setBounds(30, 20, 80, 30);
		teamPanel.add(teamLabel);
		teamNameField = new JComboBox<String>(new String[]{"选择小组","第1组","第2组","第3组","第4组","第5组","第6组","第7组","第8组"});
		teamNameField.setBounds(110, 20, 120, 30);
		teamPanel.add(teamNameField);
		JLabel teamPinLabel = new JLabel("密    码");
		teamPinLabel.setBounds(30, 70, 80, 30);
		teamPanel.add(teamPinLabel);
		teamPasswordField = new JPasswordField();
		teamPasswordField.setBounds(110, 70, 120, 30);
		teamPanel.add(teamPasswordField);
		teamLoginButton = new JButton("登    陆");
		teamLoginButton.setBounds(20, 120, 100, 30);
		teamLoginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeamService teamService = null;
				try {
					teamService = Erps.getIocInstance().get(TeamServiceImpl.class);
					int id = teamNameField.getSelectedIndex();
					String password = String.valueOf(teamPasswordField.getPassword());
					if(id==0 || password.equals("")){
						JOptionPane.showMessageDialog(null, "请输入必要信息", "提示",JOptionPane.INFORMATION_MESSAGE);
					} else {											
						if (teamService.loginCheck(id,password)) {
							Team team = teamService.getTeamById(id);
							Erps.setCurrentTeam(team);
							new MainFrame();							
							dispose();
						} else{
							JOptionPane.showMessageDialog(null, "认证失败！请重新输入", "提示",JOptionPane.ERROR_MESSAGE);
						}
						
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		teamPanel.add(teamLoginButton);
		trainButton = new JButton("练习模式");
		trainButton.setBounds(140, 120, 100, 30);
		trainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Team team = Erps.getIocInstance().get(TeamServiceImpl.class).getTeamById(8);
				Erps.setCurrentTeam(team);
				new DecisionFrame();
				dispose();
			}
		});					
		teamPanel.add(trainButton);	
		return teamPanel;			
	}
	
	/**
	 * 工作人员登陆面板
	 * @return
	 */
	private JPanel getStaffPanel(){
		staffPanel = new JPanel();
		staffPanel.setLayout(null);
		JLabel staffLabel = new JLabel("角    色");
		staffLabel.setBounds(30, 20, 80, 30);
		staffPanel.add(staffLabel);
		staffNameField = new JComboBox<String>(new String[]{"选择角色","客户","银行","供应商","设备商","教师"});
		staffNameField.setBounds(110, 20, 120, 30);
		staffPanel.add(staffNameField);
		JLabel staffPinLabel = new JLabel("密    码");
		staffPinLabel.setBounds(30, 70, 80, 30);
		staffPanel.add(staffPinLabel);
		staffPasswordField = new JPasswordField();
		staffPasswordField.setBounds(110, 70, 120, 30);
		staffPanel.add(staffPasswordField);
		staffLoginButton = new JButton("登    陆");
		staffLoginButton.setBounds(70, 120, 120, 30);
		staffLoginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				StaffService staffService = null;
				try {
					staffService = Erps.getIocInstance().get(StaffServiceImpl.class);
					int id = staffNameField.getSelectedIndex();
					String password = String.valueOf(staffPasswordField.getPassword());
					if(id==0 || password.equals("")){
						JOptionPane.showMessageDialog(null, "请输入必要信息", "提示",JOptionPane.INFORMATION_MESSAGE);
					} else {											
						if (staffService.loginCheck(id,password)) {
							Staff staff = staffService.getStaffById(id);
							Erps.setCurrentStaff(staff);
							if(staff.getId()==5){  //如果教师登录
								new SystemManagementFrame(); 
							}else{
								new BusinessFrame();
							}
							dispose();
						} else{
							JOptionPane.showMessageDialog(null, "认证失败！请重新输入", "提示",JOptionPane.ERROR_MESSAGE);
						}
						
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		staffPanel.add(staffLoginButton);
		return staffPanel;
	}
}
